const requsts = require("/util/requst");

Page({
  data: {
    activeTab: 0,
    isSwipeable:false,
    tabBarInactiveBgColor:"#fff",
    tabBarActiveBgColor:"#F5F5F5",
    tabBarlineColor:"#FF5000",
    tabBarActiveTextColor:"#FF5000",
    page: 1, // 当前页数
    list: [], // 页面List数据
    mockData:6,
    lefttrue:false,
    righttrue:false,
    tabs: [],
  },
  handleChange(index) {
    my.showLoading({
      content: '加载中...',
    });
       this.setData({
        activeTab: index,
      });
      this.setData({
            list:[],
            righttrue:false
          })
      var that=this
      my.request({
        url: requsts.url+"/product/getProductInfoList?productType="+(that.data.tabs)[index].productType,
        method: 'POST',
        dataType: 'json',
        success: function(res) {
         console.log(res.data.data)
         
         that.setData({
            list:res.data.data,
            righttrue:true
          })
          my.hideLoading();
        },
        fail: function(res) {
        },
      });
   
  },
  // onChange(index) {
  //   console.log('onChange', index);
  //   this.setData({
  //     activeTab: index,
  //   });
  //     var that=this
  //     my.request({
  //       url: requsts.url+"/product/getProductInfoList?productType="+(that.data.tabs)[index].productType,
  //       method: 'POST',
  //       dataType: 'json',
  //       success: function(res) {
  //        console.log(res.data.data)
         
  //        that.setData({
  //           list:res.data.data,
  //           //prolistshiw:true
  //         })
  //         my.hideLoading();
  //       },
  //       fail: function(res) {
  //       },
  //     });
  // },
  //左边列表
  Productlist(){

      var that=this
      that.setData({
            lefttrue:false
           })
      my.request({
        url: requsts.url+"/product/getProductTypeConfigList",
        method: 'POST',
        dataType: 'json',
        success: function(res) {
         console.log(res.data.data)
         var arr=res.data.data
         for(var i =0;i<arr.length;i++){
           arr[i].anchor=i
         }
         console.log(arr)
          that.setData({
            tabs:arr,
            lefttrue:true
           })
        },
        fail: function(res) {
        },
      });
  },
  getProductInfoList(e){
      var that=this
      that.setData({
            list:[],
            righttrue:false
          })
      my.request({
        url: requsts.url+"/product/getProductInfoList?productType="+e.productType,
        method: 'POST',
        dataType: 'json',
        success: function(res) {
         console.log(res.data.data)
         
         that.setData({
            list:res.data.data,
            righttrue:true
          })
        },
        fail: function(res) {
        },
      });
  },
  onReady() {
    // 页面加载完成
    my.hideLoading();
    // if(this.data.lefttrue==true&&this.data.righttrue==true){
    //     my.hideLoading();
    // }
  },
  jumpdetial(e){
      console.log(e.currentTarget.dataset.id)
    my.navigateTo({
        url:'/page/common/goodsorder/goodsorder?productType='+e.currentTarget.dataset.id,
    })
  },
  onLoad(e){
    this.Productlist()
    this.getProductInfoList(e)
    this.setData({
     activeTab:e.id,
    })
    // 页面加载
    my.showLoading({
      content: '加载中...',
    });
  },
  onItemClick(ev) {
    // my.alert({
    //   content: ev.detail.index,
    // });
  },
  /**
   * scroll-view滑到底部触发事件
   * @method scrollMytrip
   */
  async scrollMytrip() {
    try {
      const { page, list, } = this.data;
      // 判断是否还有数据需要加载
      if (list.length < mockTotal) {
        const newPage = page + 1;
        this.mySchedulde(newPage);
      }
    } catch (e) {
      console.log('scrollMytrip执行异常:', e);
    }
  },
  /**
   * 请求服务端查询数据并渲染页面
   * @method mySchedulde
   * @param {int} page 分页,默认第1页
   */
  async mySchedulde(page = 1) {
    try {
      let list = this.data.list;
      // 模拟请求拿到数据进行更新data
      setTimeout(() => {
        let data = this.data.mockData;
        for (let i = 0; i < data.length; i++) {
          let newObj = { ...data[i], remarksa: `我是第${page}页` };
          list.push(newObj);
        }
        this.setData({
          list,
          page,
        });
      }, 800);
    } catch (e) {
      console.log('mySchedulde执行异常:', e);
    }
  }
});